<template>
  <el-row :gutter="20" class="list-page">
    <el-col :span="5" style="display: flex; flex-direction: column; height: 100%; padding: 20px" class="category">
      <div class="tree">
        <el-tree class="tree" :data="flowTree" :props="{ children: 'sons', label: 'label' }" :default-expand-all="true"
          :expand-on-click-node="false" @node-click="treeClick" highlight-current></el-tree>
      </div>
    </el-col>
    <el-col :span="19">
      <div>
        <div class="header">
          <span style="margin-left: 20px">表单搜索</span>
          <el-input placeholder="请输入关键字搜索表单" v-model="req.keyword" style="width: 200px; margin-left: 10px"></el-input>
          <el-button type="primary" style="margin-left: 34px" size="small" @click="getMyForms()">
            搜索
          </el-button>
          <el-button type="success" style="margin-left: 10px" size="small" v-if="excelReq.flowId"
            @click="outputExcelVis = true">
            导出报表
          </el-button>
        </div>
        <div class="main">
          <el-table style="overflow: auto" height="100%" :data="res.Items" stripe border highlight-current-row>
            <el-table-column label="流程" width="180px">
              <template slot-scope="scope">
                <div style="margin-bottom: 5px">
                  <el-tag type="primary" v-if="scope.row.WFM_Status == 1">
                    {{ scope.row.WFM_StatusDesc }}[{{ scope.row.FRS_Name }}]
                  </el-tag>
                  <el-tag type="success" v-else-if="scope.row.WFM_Status == 2">
                    {{ scope.row.WFM_StatusDesc }}
                  </el-tag>
                  <el-tag type="danger" v-else>
                    {{ scope.row.WFM_StatusDesc }}
                  </el-tag>
                </div>
                <span>{{ scope.row.FLOW_Name }}</span>
                <!-- <el-link type="primary">状态：{{ scope.row.FRS_Name }}</el-link> -->
              </template>
            </el-table-column>
            <el-table-column label="发起人及日期" width="180px">
              <template slot-scope="scope">
                <div>{{ scope.row.USR_Name }}</div>
                发起日期：{{ scope.row.WFM_CreateTime | formatDate }}
                <div v-if="scope.row.WFM_FinishTime">完结日期：{{ scope.row.WFM_FinishTime | formatDate }}</div>
              </template>
            </el-table-column>
            <!-- <el-table-column label="表单名" width="300px">
              <template slot-scope="scope">
                <span>{{ scope.row.WFM_FormDataName }}</span>
              </template>
            </el-table-column> -->
            <el-table-column label="表单信息">
              <template slot-scope="scope">
                <div v-if="scope.row.WFM_FormDataCode">
                  搜寻码：{{ scope.row.WFM_FormDataCode }}
                </div>
                <div v-for="item in scope.row.FieldValues" :key="item.FFV_ID">
                  {{ item.FF_DisplayName }}: {{ item.FFV_DisplayValue }}
                </div>
              </template>
            </el-table-column>
            <!-- <el-table-column label="属性1" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop1Name }}: {{ scope.row.WFM_Prop1 }}
              </template>
            </el-table-column>
            <el-table-column label="属性2" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop2Name }}: {{ scope.row.WFM_Prop2 }}
              </template>
            </el-table-column>
            <el-table-column label="属性3" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop3Name }}: {{ scope.row.WFM_Prop3 }}
              </template>
            </el-table-column>
            <el-table-column label="属性4" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop4Name }}: {{ scope.row.WFM_Prop4 }}
              </template>
            </el-table-column> -->
            <el-table-column fixed="right" label="操作" width="150px">
              <template slot-scope="scope">
                <el-button type="text" icon="el-icon-search" @click="toFormPage(scope.row.WFM_ID)">
                  查看
                </el-button>
                <el-button type="text" icon="el-icon-key" @click="hideForm(scope.row.WFM_ID)">
                  隐藏
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="width: 100%; height: 40px">
          <el-pagination layout="total,prev,pager,next" style="float: right" background small
            :current-page.sync="req.pageIndex" :total="res.Total" :page-size="req.pageSize" :page-sizes="[10, 15, 20]"
            @size-change="
              (val) => {
                req.pageSize = val
                getMyForms()
              }
            " @current-change="getMyForms()"></el-pagination>
        </div>
      </div>
      <el-dialog title="导出报表" :visible.sync="outputExcelVis" width="600px" :modal-append-to-body="false" center>
        <el-form label-width="100px" style="margin-top: 20px; width: 90%">
          <el-form-item label="关键字">
            <el-input placeholder="请输入关键字" v-model="excelReq.keyword"
              style="width: 350px; margin-left: 10px"></el-input>
          </el-form-item>
          <el-form-item label="发起日期">
            <el-date-picker type="date" v-model="excelReq.createDate1" placeholder="请选择起始日期"
              value-format="yyyy-MM-dd HH:mm:ss" style="width: 160px; margin-left: 10px"></el-date-picker>
            至
            <el-date-picker type="date" v-model="excelReq.createDate2" placeholder="请选择截止日期"
              value-format="yyyy-MM-dd HH:mm:ss" style="width: 160px; margin-left: 10px"></el-date-picker>
          </el-form-item>
          <el-form-item label="完结日期">
            <el-date-picker type="date" v-model="excelReq.finishDate1" placeholder="请选择起始日期"
              value-format="yyyy-MM-dd HH:mm:ss" style="width: 160px; margin-left: 10px"></el-date-picker>
            至
            <el-date-picker type="date" v-model="excelReq.finishDate2" placeholder="请选择截止日期"
              value-format="yyyy-MM-dd HH:mm:ss" style="width: 160px; margin-left: 10px"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="margin-left: 10px" size="small" @click="outputExcel">
              确定
            </el-button>
            <el-button type="text" style="margin-left: 10px" size="small" @click="outputExcelVis = false">
              取消
            </el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-col>
  </el-row>
</template>
<script>
import {
  getMyFlowTree,
  getMyForms,
  hideForm,
} from '@/api/flowCenter/form/formManage'
import { baseURL, tokenName, empIdName } from '@/config'
import axios from 'axios'
export default {
  data: function () {
    return {
      flowTree: [],
      req: {
        keyword: null,
        flowId: null,
        pageIndex: 1,
        pageSize: 10,
      },
      res: {
        Items: [],
        Total: 0,
      },
      excelReq: {
        keyword: null,
        createDate1: null,
        createDate2: null,
        finishDate1: null,
        finishDate2: null,
        flowId: null,
      },
      outputExcelVis: false,
    }
  },
  methods: {
    treeClick(item) {
      this.curNode = item
      this.getMyForms()
    },
    hideForm(formId) {
      this.$confirm(
        '是否隐藏该表单，隐藏后，该表单在“公司表单”中将不再显示?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        hideForm(formId).then(() => {
          this.$message('已隐藏')
          this.getMyForms()
        })
      })
    },
    getMyFlowTree() {
      getMyFlowTree().then(({ data }) => {
        this.flowTree = data
        this.getMyForms()
      })
    },
    getMyForms() {
      if (this.curNode) {
        switch (this.curNode.level) {
          case 0: {
            this.req.bsnId = null
            this.req.flowId = null
            this.excelReq.flowId = null
            break
          }
          case 1: {
            this.req.bsnId = this.curNode.id
            this.req.flowId = null
            this.excelReq.flowId = null
            break
          }
          case 2: {
            this.req.flowId = this.curNode.id
            this.excelReq.flowId = this.curNode.id
            break
          }
        }
        getMyForms(this.req).then(({ data }) => {
          this.res = data
        })
      }
    },
    toFormPage(formId) {
      this.$router.push({
        name: 'formInfoPage',
        query: { formId },
      })
    },
    outputExcel() {
      if (
        this.excelReq.keyword == null &&
        this.excelReq.createDate1 == null &&
        this.excelReq.createDate2 == null &&
        this.excelReq.finishDate1 == null &&
        this.excelReq.finishDate2 == null
      ) {
        this.$message('请选择导出日期')
        return
      }
      var config = {
        baseURL,
        headers: {},
        responseType: 'blob',
      }
      config.headers[tokenName] = this.$store.getters['user/accessToken']
      config.headers[empIdName] = this.$store.getters['user/curEmp'].EMP_ID
      axios
        .post('/api/Form/FormManage/OutputFormExcel', this.excelReq, config)
        .then((response) => {
          this.outputExcelVis = false
          var blob = new Blob([response.data])
          var downloadElement = document.createElement('a')
          var href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.href = href
          downloadElement.download = this.curNode.label + '.xls' //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
        })
        .catch((error) => {
          console.log('response: ', error)
        })
    },
  },
  created: function () {
    var data = this.$store.getters['pagination/getRoutePage'](this.$route)
    Object.assign(this.$data, data)
    this.getMyFlowTree()
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch('pagination/setRoutePage', {
      route: this.$route,
      pageData: this.$data,
    })
    next()
  },
}
</script>

<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}

.main {
  height: calc(75vh - 60px);
  overflow: auto;
}
</style>